//
//  FeedbackViewController.m
//  xpkc
//
//  Created by TopSage iOS on 14-8-21.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "FeedbackViewController.h"
#import "UserMessage.h"
@interface FeedbackViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@end

@implementation FeedbackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"意见反馈";
        [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                 [UIColor colorWithRed:255/255.0f green:159/255.f blue:0/255.f alpha:1], NSForegroundColorAttributeName,
                                                 nil] forState:UIControlStateSelected];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    
//    [self creatNavItem];
    if (IsIOS7) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    _feedbackView=[[FeedbackView alloc]initWithFrame:self.view.bounds];
    [_feedbackView.cameraButton addTarget:self action:@selector(cameraButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_feedbackView];
    [GetPhoneInfo getInfo];
    _feedbackView.deviceLabel.text = [NSString stringWithFormat:@"设备型号:%@ 系统版本:%@ 客户端版本:%@",[[UIDevice currentDevice] model],[[UIDevice currentDevice] systemVersion],[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    [_feedbackView.button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)cameraButtonClicked {
    
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    [sheet showInView:self.view];
    
}

#pragma mark - =========添加图片相关代理===========
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    if([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [_feedbackView.cameraButton setImage:image forState:UIControlStateNormal];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex != 2) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        if(buttonIndex == 0) {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:picker animated:YES completion:^{
                    
                }];
            } else {
                ShowAlertMsg(@"相机不可用!");
            }
        } else if (buttonIndex == 1) {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:picker animated:YES completion:^{
                    
                }];
            } else {
                ShowAlertMsg(@"无法调用相册库!");
            }
        }
    }
}

//完成编辑收键盘
- (void)finish{
    [_feedbackView.textView resignFirstResponder];
}

-(void)backtoset{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *   点击回收键盘
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_feedbackView.textView resignFirstResponder];
}
/**
 *   点击，提交反馈
 */
- (void)btnClick{
    NSString *content = _feedbackView.textView.text;
    content = [NSString stringWithFormat:@"系统信息:%@\n反馈信息:%@",_feedbackView.deviceLabel.text,content];//反馈信息整合;
//    if ([content isEqual:@""] || content ==nil || [content isEqualToString:@"\n"])
    if ([_feedbackView.textView.text isEqual:@""] || _feedbackView.textView.text ==nil || [_feedbackView.textView.text isEqualToString:@"\n"])
        
    {
         NSLog(@"%@========",_feedbackView.textView.text);
        NSString *message=@"请输入反馈内容";
        ShowAlertMsg(message);
    }else{
        NSString *module = @"setting";
        NSString *interface = @"feedback";
        NSString *timeString = [GetTime getTime];
        NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
        NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
        NSDictionary *parameter = @{@"time":timeString,@"username":[UserMessage sharedUserMessage].userId,@"msg":content};
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            
            NSLog(@"==========%@===========",dic);
                   NSString *message=@"提交成功";
                    ShowAlertMsg(message);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //
            logdebug(@"%@",error.localizedDescription);
        }];

//        NSString *timeString = [NSString stringWithFormat:@"%@", [GetTime getTime]];
//        NSString *string1 = [NSString stringWithFormat:@"/Setting/feedBack/key/0b6f2f666ddfd0e56e4d566f12a0f353/time/%@", timeString];
//        NSString *MD5string = [string1 md5];
//        NSString *url1 = [NSString stringWithFormat:@"http://www.studypay.com/MAPI/index.php/Setting/feedBack/hash/%@/appid/1", MD5string];
//        //POST请求
//        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//        NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:timeString,@"time",content,@"msg", nil];
//        //提交反馈
//        [manager POST:url1 parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//            
//        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//            //打印错误信息
//            logdebug(@"error:%@",error.localizedDescription);
//        }];
//        NSString *message=@"提交成功";
//        ShowAlertMsg(message);
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
